import _toConsumableArray from 'babel-runtime/helpers/toConsumableArray';
import _extends from 'babel-runtime/helpers/extends';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
/**
 * 数据字典下拉选择组件
 * @author: ljq
 * @since: 2018-10-26 11:55:09
 */
import * as React from 'react';
import { Select } from 'antd';
var Option = Select.Option;

var DictSelect = function (_React$Component) {
    _inherits(DictSelect, _React$Component);

    function DictSelect(props) {
        _classCallCheck(this, DictSelect);

        var _this = _possibleConstructorReturn(this, (DictSelect.__proto__ || Object.getPrototypeOf(DictSelect)).call(this, props));

        _this.getSelect = function () {
            var _this$props = _this.props,
                type = _this$props.type,
                defaultValue = _this$props.defaultValue,
                style = _this$props.style,
                dictkey = _this$props.dictkey,
                filterkeys = _this$props.filterkeys,
                valueInLabel = _this$props.valueInLabel,
                order = _this$props.order,
                storagetype = _this$props.storagetype;

            var items = void 0;
            if (storagetype && storagetype === "localStorage") {
                items = localStorage['dict_' + dictkey];
            } else {
                items = sessionStorage['dict_' + dictkey];
            }
            if (!items) {
                if (type == 'text') {
                    return React.createElement('span', null);
                } else {
                    return React.createElement(Select, _extends({}, _this.props, { className: 'dict-select' }));
                }
            }
            items = JSON.parse(items);
            if (filterkeys) {
                items = items.filter(function (item) {
                    return filterkeys.includes(item.itemKey);
                });
            }
            if (order) {
                var orderItems = order.map(function (itemKey) {
                    return items.filter(function (item) {
                        return item.itemKey === itemKey;
                    })[0];
                });
                orderItems.push.apply(orderItems, _toConsumableArray(items.filter(function (item) {
                    return !order.includes(item.itemKey);
                })));
                items = orderItems;
            }
            if (type == 'text') {
                if (!defaultValue) return React.createElement('span', null);
                for (var i = 0; i < items.length; i += 1) {
                    var item = items[i];
                    if (item.itemKey === defaultValue) {
                        return React.createElement(
                            'span',
                            { 'data-itemkey': item.itemKey, style: style },
                            item.itemName
                        );
                    }
                }
                return React.createElement('span', null);
            } else {
                return React.createElement(
                    Select,
                    _extends({ dropdownMatchSelectWidth: false }, _this.props, { className: 'dict-select' }),
                    items.map(function (item) {
                        return React.createElement(
                            Option,
                            { key: item.itemKey, value: item.itemKey },
                            valueInLabel && item.itemKey + ' - ',
                            item.itemName
                        );
                    })
                );
            }
        };
        // 校验
        var dictkey = _this.props.dictkey;

        if (!dictkey) {
            throw new Error('Dict.Select组件需要属性：dictkey');
        }
        return _this;
    }

    _createClass(DictSelect, [{
        key: 'render',
        value: function render() {
            return this.getSelect();
        }
    }]);

    return DictSelect;
}(React.Component);

export default DictSelect;